﻿Imports Microsoft.Win32
Public Class frmSettings

#Region " Declerations  "

    Dim C1 As System.Drawing.Color = Color.LightSkyBlue
    Dim C2 As System.Drawing.Color = Color.LightGreen
    Dim C3 As System.Drawing.Color = Color.Orange
    Dim C4 As System.Drawing.Color = Color.Red

    Dim tempC1 As System.Drawing.Color = My.Settings.color1
    Dim tempC2 As System.Drawing.Color = My.Settings.color2
    Dim tempC3 As System.Drawing.Color = My.Settings.color3
    Dim tempC4 As System.Drawing.Color = My.Settings.color4

    Dim COOL_MAX As Integer = 39
    Dim ACCEPT_MIN As Integer = 40
    Dim ACCEPT_MAX As Integer = 59
    Dim HOT_MIN As Integer = 60
    Dim HOT_MAX As Integer = 79
    Dim CRITICAL_MIN As Integer = 80

    Dim dr1, dr2, dr3, dr4 As DialogResult

    Dim RK As RegistryKey
    Dim Regpath As String = "Software\Microsoft\Windows\CurrentVersion\Run"
    Dim AppName As String = "Aurora GPU Monitor"
    Dim LOS As Boolean

#End Region

#Region " Functions "

    Private Sub loadSettings()
        Try
            My.Settings.Reload()

            Check_LoadatStartup()
            Check_StartMinimized()
            Check_showWelcomScreen()

            txtCool.ForeColor = My.Settings.color1
            txtAcceptable.ForeColor = My.Settings.color2
            txtHot.ForeColor = My.Settings.color3
            txtCritical.ForeColor = My.Settings.color4
            txtCoolMin.Text = My.Settings.coolMax
            txtAceptMin.Text = My.Settings.acceptableMin
            txtAceptMax.Text = My.Settings.acceptableMax
            txtHotMin.Text = My.Settings.hotMin
            txtHotMax.Text = My.Settings.hotMax
            txtCriticalMin.Text = My.Settings.criticalMin

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Check_LoadatStartup()
        Try
            RK = Registry.CurrentUser.OpenSubKey(Regpath, True)
            If RK.GetValue("Aurora GPU Monitor") = Nothing Then
                ChkBxStartUp.Checked = False
                LOS = False
                ChkBxMinimize.Enabled = False
               
            Else
                ChkBxStartUp.Checked = True
                LOS = True
                ChkBxMinimize.Enabled = True
            End If
            Label7.Text = My.Settings.startup
            Label8.Text = My.Settings.minimize
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Check_StartMinimized()
        Try
            If (My.Settings.startup = True AndAlso My.Settings.minimize = True) Then
                ChkBxMinimize.Checked = True
            ElseIf (My.Settings.startup = True AndAlso My.Settings.minimize = False) Then
                ChkBxMinimize.Checked = False
            Else
                With ChkBxMinimize
                    .Checked = False
                    .Enabled = False
                End With
            End If
            Label8.Text = My.Settings.minimize
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Check_showWelcomScreen()
        Try
            If My.Settings.remember = False Then
                ChkBxWelcomeScreen.Checked = True
            ElseIf My.Settings.remember = True Then
                ChkBxWelcomeScreen.Checked = False
            End If
            Label9.Text = My.Settings.remember
        Catch ex As Exception

        End Try
    End Sub

    Private Sub setStartup()
        Try

            RK = Registry.CurrentUser.OpenSubKey(Regpath, True)
            If RK.GetValue("Aurora GPU Monitor") = Nothing Then
                If LOS = False Then
                    Try
                        RK = Registry.CurrentUser.OpenSubKey(Regpath, True)
                        RK.SetValue(AppName, MyPath(), RegistryValueKind.String)
                        ChkBxStartUp.Checked = True
                        My.Settings.startup = True
                        LOS = True
                        
                        ChkBxMinimize.Enabled = True
                    Catch ex As Exception
                        MsgBox(vbCrLf & ex.Message, vbCritical, "An error occurred")
                    End Try
                End If

            Else
                If LOS = True Then
                    Try
                        My.Computer.Registry.CurrentUser.OpenSubKey(Regpath, True).DeleteValue("Aurora GPU Monitor")
                        LOS = False
                        My.Settings.startup = False
                        ChkBxStartUp.Checked = False
                        My.Settings.minimize = False
                        ChkBxMinimize.Checked = False
                        ChkBxMinimize.Enabled = False
                    Catch ex As Exception
                        MsgBox(vbCrLf & ex.Message, vbCritical, "An error occurred")
                    End Try
                End If

            End If
            Label7.Text = My.Settings.startup
            Label8.Text = My.Settings.minimize
        Catch

        End Try
	End Sub

	Public Sub resetStartup_Settings()
		Try

			RK = Registry.CurrentUser.OpenSubKey(Regpath, True)

			If RK.GetValue("Aurora GPU Monitor") = Nothing Then
			Else

				Try
					My.Computer.Registry.CurrentUser.OpenSubKey(Regpath, True).DeleteValue("Aurora GPU Monitor")
					LOS = False
					My.Settings.startup = False
					ChkBxStartUp.Checked = False
					My.Settings.minimize = False
					ChkBxMinimize.Checked = False
					ChkBxMinimize.Enabled = False
				Catch ex As Exception
					MsgBox(vbCrLf & ex.Message, vbCritical, "An error occurred")
				End Try


			End If
			Label7.Text = My.Settings.startup
			Label8.Text = My.Settings.minimize
		Catch

		End Try
	End Sub

    Private Sub setTrayMinimize()
        Try
            If (My.Settings.startup = True AndAlso ChkBxMinimize.Enabled = True) Then
                If My.Settings.minimize = True Then
                    My.Settings.minimize = False
                    ChkBxMinimize.Checked = False
                ElseIf My.Settings.minimize = False Then
                    My.Settings.minimize = True
                    My.Settings.remember = True
                    ChkBxWelcomeScreen.Checked = False
                    ChkBxMinimize.Checked = True

                End If
                My.Settings.Save()
            Else
                MsgBox("Choose to Start at Boot First ", MsgBoxStyle.OkOnly, "Warning")
            End If
            Label8.Text = My.Settings.minimize
        Catch ex As Exception

        End Try
    End Sub

    Private Sub setWelcomeScreenOption()
        Try
            If My.Settings.minimize = False Then
                If My.Settings.remember = True Then
                    My.Settings.remember = False
                    ChkBxWelcomeScreen.Checked = True
                ElseIf My.Settings.remember = False Then
                    My.Settings.remember = True
                    ChkBxWelcomeScreen.Checked = False
                End If
                My.Settings.Save()
            Else
                If ChkBxWelcomeScreen.Checked = True Then
                    ChkBxWelcomeScreen.Checked = False
                End If
                MsgBox("Cannot Show Welcome Screen When Minimized")
            End If

            Label9.Text = My.Settings.remember
        Catch ex As Exception

        End Try
    End Sub

    Private Function MyPath() As String

        Dim AppLocation As String = Application.StartupPath & "\" & My.Application.Info.AssemblyName & ".exe"

        Return AppLocation

    End Function

    Public Function checkValidity_Range(ByVal loadMySettings As Boolean) As Boolean
        Try
            If loadMySettings = True Then
                loadSettings()
            End If
            If ((CInt(txtCoolMin.Text) < CInt(txtAceptMin.Text) AndAlso CInt(txtCoolMin.Text) < CInt(txtAceptMax.Text) AndAlso CInt(txtCoolMin.Text) < CInt(txtHotMin.Text) AndAlso CInt(txtCoolMin.Text) < CInt(txtHotMax.Text) AndAlso CInt(txtCoolMin.Text) < CInt(txtCriticalMin.Text)) AndAlso (CInt(txtAceptMin.Text) < CInt(txtAceptMax.Text) AndAlso CInt(txtAceptMin.Text) < CInt(txtHotMin.Text) AndAlso CInt(txtAceptMin.Text) < CInt(txtHotMax.Text) AndAlso CInt(txtAceptMin.Text) < CInt(txtCriticalMin.Text)) AndAlso (CInt(txtAceptMax.Text) < CInt(txtHotMin.Text) AndAlso CInt(txtAceptMax.Text) < CInt(txtHotMax.Text) AndAlso CInt(txtAceptMax.Text) < CInt(txtCriticalMin.Text)) AndAlso (CInt(txtHotMin.Text) < CInt(txtHotMax.Text) AndAlso CInt(txtHotMin.Text) < CInt(txtCriticalMin.Text)) AndAlso (CInt(txtHotMax.Text) < CInt(txtCriticalMin.Text))) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception

        End Try
        Return False

    End Function

    Public Function checkValidity_Color(ByVal loadMySettings As Boolean) As Boolean
        Try
            If loadMySettings = True Then
                loadSettings()
            End If
            If ((tempC1 <> tempC2 AndAlso tempC1 <> tempC3 AndAlso tempC1 <> tempC4) AndAlso (tempC2 <> tempC3 AndAlso tempC2 <> tempC4) AndAlso (tempC3 <> tempC4)) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception

        End Try

        Return False

    End Function

    Public Sub setDefaults_Range()
        Try

            My.Settings.coolMax = COOL_MAX
            My.Settings.acceptableMin = ACCEPT_MIN
            My.Settings.acceptableMax = ACCEPT_MAX
            My.Settings.hotMin = HOT_MIN
            My.Settings.hotMax = HOT_MAX
            My.Settings.criticalMin = CRITICAL_MIN
            My.Settings.Save()

            txtCoolMin.Text = My.Settings.coolMax
            txtAceptMin.Text = My.Settings.acceptableMin
            txtAceptMax.Text = My.Settings.acceptableMax
            txtHotMin.Text = My.Settings.hotMin
            txtHotMax.Text = My.Settings.hotMax
            txtCriticalMin.Text = My.Settings.criticalMin

        Catch ex As Exception

        End Try


    End Sub

    Public Sub setDefaults_Color()
        Try

            My.Settings.color1 = C1
            My.Settings.color2 = C2
            My.Settings.color3 = C3
            My.Settings.color4 = C4
            My.Settings.Save()

            txtCool.ForeColor = My.Settings.color1
            txtAcceptable.ForeColor = My.Settings.color2
            txtHot.ForeColor = My.Settings.color3
            txtCritical.ForeColor = My.Settings.color4

        Catch ex As Exception

        End Try


    End Sub

#End Region

#Region " Form "

	Private Sub frmSettings_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		Try
			If e.KeyCode = Keys.Escape Then
				timrEnd.Enabled = True
			End If
		Catch ex As Exception

		End Try
	End Sub

	Private Sub frmSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		Try
			Me.Hide()
			Me.Icon = My.Resources.GPU
			loadSettings()
			Me.Opacity = 0
			Me.Show()
			timrStart.Enabled = True
		Catch ex As Exception

		End Try

	End Sub

	Private Sub frmSettings_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		Try
			timrEnd.Enabled = True
			Exit Sub
		Catch ex As Exception

		End Try
	End Sub

#End Region

#Region " GUI "

    Private Sub btnDefaultsRange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefaultsRange.Click
        Try
            setDefaults_Range()
            MsgBox("Application Defaults Loaded succesfully", MsgBoxStyle.OkOnly, "Settings - Temperature  Ranges")
        Catch ex As Exception
            MsgBox("Application Defaults loading failded", MsgBoxStyle.OkOnly, "Settings ")
        End Try
    End Sub

    Private Sub btnDefaultsColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefaultsColor.Click
        Try
            setDefaults_Color()
            MsgBox("Application Defaults Loaded succesfully", MsgBoxStyle.OkOnly, "Settings - Colors")
        Catch ex As Exception
            MsgBox("Application Defaults loading failded", MsgBoxStyle.OkOnly, "Settings ")
        End Try
    End Sub

    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Try
            If checkValidity_Range(False) = True Then

                My.Settings.coolMax = CInt(txtCoolMin.Text)
                My.Settings.acceptableMin = CInt(txtAceptMin.Text)
                My.Settings.acceptableMax = CInt(txtAceptMax.Text)
                My.Settings.hotMin = CInt(txtHotMin.Text)
                My.Settings.hotMax = CInt(txtHotMax.Text)
                My.Settings.criticalMin = CInt(txtCriticalMin.Text)
                My.Settings.Save()
                txtCoolMin.Text = My.Settings.coolMax
                txtAceptMin.Text = My.Settings.acceptableMin
                txtAceptMax.Text = My.Settings.acceptableMax
                txtHotMin.Text = My.Settings.hotMin
                txtHotMax.Text = My.Settings.hotMax
                txtCriticalMin.Text = My.Settings.criticalMin

                MsgBox("Temperature Settings Saved Successfully", MsgBoxStyle.OkOnly, "Settings")
            Else
                MsgBox("Invalid temperature limits detected, please retry ", MsgBoxStyle.OkOnly + vbCritical, "Warning")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ChkBxStartUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkBxStartUp.Click
        Try
            setStartup()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ChkBxMinimize_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkBxMinimize.Click
        Try
            setTrayMinimize()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ChkBxWelcomeScreen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkBxWelcomeScreen.Click
        Try
            setWelcomeScreenOption()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnCool_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCool.Click
        Try

            dr1 = colorDig.ShowDialog()
            If dr1 = DialogResult.OK Then
                If colorDig.Color <> Color.Black Then
                    tempC1 = colorDig.Color
                    If checkValidity_Color(False) = True Then
                        My.Settings.color1 = tempC1
                        My.Settings.Save()
                        My.Settings.Reload()
                        txtCool.ForeColor = My.Settings.color1
                    Else
                        MsgBox("Similar Colors Detected  ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                    End If

                ElseIf colorDig.Color = Color.Black Then
                    MsgBox("Cannot set text color to black ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
        Try
            dr2 = colorDig.ShowDialog()
            If dr2 = DialogResult.OK Then
                If colorDig.Color <> Color.Black Then
                    tempC2 = colorDig.Color
                    If checkValidity_Color(False) = True Then
                        My.Settings.color2 = tempC2
                        My.Settings.Save()
                        My.Settings.Reload()
                        txtAcceptable.ForeColor = My.Settings.color2
                    Else
                        MsgBox("Similar Colors Detected  ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                    End If
                ElseIf colorDig.Color = Color.Black Then
                    MsgBox("Cannot set text color to black ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnHot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHot.Click
        Try
            dr3 = colorDig.ShowDialog()
            If dr3 = DialogResult.OK Then
                If Not colorDig.Color <> Color.Black Then
                    tempC3 = colorDig.Color
                    If checkValidity_Color(False) = True Then
                        My.Settings.color3 = tempC3
                        My.Settings.Save()
                        My.Settings.Reload()
                        txtHot.ForeColor = My.Settings.color3
                    Else
                        MsgBox("Similar Colors Detected  ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                    End If
                ElseIf colorDig.Color = Color.Black Then
                    MsgBox("Cannot set text color to black ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnCritical_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCritical.Click
        Try
            dr4 = colorDig.ShowDialog()
            If dr4 = DialogResult.OK Then
                If colorDig.Color <> Color.Black Then
                    tempC4 = colorDig.Color
                    If checkValidity_Color(False) = True Then
                        My.Settings.color4 = tempC4
                        My.Settings.Save()
                        My.Settings.Reload()
                        txtCritical.ForeColor = My.Settings.color4
                    Else
                        MsgBox("Similar Colors Detected  ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                    End If

                ElseIf colorDig.Color = Color.Black Then
                    MsgBox("Cannot set text color to black ", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Warning")
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtCoolMin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCoolMin.KeyPress
        Try
            If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
              AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "," Then
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
                e.Handled = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtAceptMin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAceptMin.KeyPress
        Try
            If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
              AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "," Then
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
                e.Handled = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtAceptMax_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAceptMax.KeyPress
        Try
            If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
              AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "," Then
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
                e.Handled = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtHotMin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtHotMin.KeyPress
        Try
            If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
              AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "," Then
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
                e.Handled = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtHotMax_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtHotMax.KeyPress
        Try
            If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
              AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "," Then
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
                e.Handled = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtCriticalMin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCriticalMin.KeyPress
        Try
            If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
              AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> "," Then
                My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
                e.Handled = True
            End If
        Catch ex As Exception

        End Try
    End Sub


#End Region

#Region " Timers "

	Private Sub timrStart_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrStart.Tick
		Try
			Me.Opacity = Me.Opacity + 0.1
			If Me.Opacity >= 1 Then
				timrStart.Enabled = False
			End If
		Catch ex As Exception

		End Try
	End Sub

	Private Sub timrEnd_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timrEnd.Tick
		Try
			Me.Opacity = Me.Opacity - 0.1
			If Me.Opacity <= 0 Then
				timrStart.Enabled = False
				Me.Close()
			End If
		Catch ex As Exception

		End Try
	End Sub

#End Region

End Class